<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="目前流程">
        <el-select v-model="registerQuery.status" placeholder="请选择">
          <el-option :key="0" :value="0" label="简历测评"/>
          <el-option :key="1" :value="1" label="笔试测评"/>
          <el-option :key="2" :value="2" label="面试测评"/>
          <el-option :key="3" :value="3" label="录用"/>
        </el-select>
      </el-form-item>
      <el-form-item label="目前状态">
        <el-select v-model="registerQuery.result" placeholder="请选择">
          <el-option :key="0" :value="0" label="正在进行"/>
          <el-option :key="1" :value="1" label="通过"/>
          <el-option :key="2" :value="2" label="淘汰"/>
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <el-button type="primary" @click="print_register_detail">下载招聘详情</el-button>
    <el-button type="success" @click="print_all_resume">下载所有简历</el-button>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="registers"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (current - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="姓名" width="200" >
        <template slot-scope="scope">
          <router-link :to="'/user/detail/'+scope.row.userId">
            {{ scope.row.userName }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="岗位名称" width="255">
        <template slot-scope="scope">
          <router-link :to="'/job/detail/'+scope.row.jobId">
            {{ scope.row.jobName }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="目前流程" width="120" >
        <template slot-scope="scope">
          {{ get_status(scope.row.status) }}
        </template>
      </el-table-column>

      <el-table-column label="目前状态" width="120" >
        <template slot-scope="scope">
          {{ get_result(scope.row.result) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">

          <div v-if="scope.row.status == 3">
            已通过
          </div>
          <div v-else-if="scope.row.result == 2">
            已淘汰
          </div>
          <div v-else>
            <el-button type="success" size="mini" @click="publish_register(scope.row.id, 1)">通过</el-button>
            <el-button type="danger" size="mini" @click="publish_register(scope.row.id, 0)">淘汰</el-button>
          </div>

        </template>
      </el-table-column>

      <el-table-column label="下载简历" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="print_resume(scope.row.userId)">下载</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>

import register from '@/api/hr/register'
import resume from '@/api/hr/resume'
export default {
  data() {
    return {
      listLoading: false,
      registers: [],
      current: 1,
      limit: 5,
      total: 10,
      registerQuery: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(current = 1) {
      this.current = current
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.registerQuery.jobId = id
        register.get_register_page(this.current, this.limit, this.registerQuery)
          .then(response => {
            this.registers = response.data.registers
            this.total = response.data.total
          }).catch(response => {
            console.log('error')
          })
      } else {
        this.registers = {}
      }
    },
    resetData() {
      this.registerQuery = {}
      this.getList()
    },
    get_status(status) {
      switch (status) {
        case 0:
          return '简历测评'
        case 1:
          return '笔试测评'
        case 2:
          return '面试测评'
        case 3:
          return '录用'
      }
    },
    get_result(result) {
      switch (result) {
        case 0:
          return '正在进行'
        case 1:
          return '通过'
        case 2:
          return '淘汰'
      }
    },
    print_resume(userId) {
      resume.print_resume(userId)
        .then(response => {
          this.$message({
            type: 'success',
            message: '下载成功!'
          })
        })
    },
    print_register_detail() {
      register.print_register_detail(this.$route.params.id)
        .then(response => {
          this.$message({
            type: 'success',
            message: '下载成功!'
          })
        })
    },
    print_all_resume() {
      resume.print_all_resume(this.$route.params.id)
        .then(response => {
          this.$message({
            type: 'success',
            message: '下载成功!'
          })
        })
    },
    publish_register(id, flag) {
      register.publish_register(id, flag)
        .then(response => {
          this.getList()
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        })
    }
  }
}
</script>
